import React from 'react';
import { Button, Drawer, Popconfirm, Space, Table } from 'antd';
import DeptForm from './DeptForm'; // 引入部门表单组件

class Demo2 extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      tableData: [],
      drawerVisible: false, // 控制抽屉是否可见
      currentRecord: null, // 当前操作的记录
    };
  }

  refreshData = () => {
    const url = 'http://1.94.134.166:8805/dept/list?parentId=0';
    const config = {
      method: 'GET',
      headers: {
        Authorization: localStorage.getItem('Authorization'), // 从localStorage获取认证信息
      },
    };

    fetch(url, config)
      .then((response) => response.json())
      .then((res) => {
        console.log(res);
        this.setState({ tableData: res.data }); // 更新表格数据
      });
  };

  // 组件挂载后调用刷新数据的方法
  componentDidMount() {
    this.refreshData();
  }

  // 删除部门
  handleDelete = (record) => {
    const url = 'http://1.94.134.166:8805/dept/' + record.deptId;
    const config = {
      method: 'DELETE',
      headers: {
        Authorization: localStorage.getItem('Authorization'),
      },
    };

    fetch(url, config)
      .then((response) => response.json())
      .then((res) => {
        console.log(res);
        this.refreshData(); // 删除后刷新数据
      });
  };

  // 编辑部门
  handleEdit = (record) => {
    this.setState({
      drawerVisible: true,
      currentRecord: record, // 当前操作的记录
    });
  };

  columns = [
    {
      title: '序号',
      key: 'index',
      render: (text, record, index) => {
        return <span>{index + 1}</span>;
      },
    },
    {
      title: '部门名称',
      dataIndex: 'deptName',
      key: 'deptName',
    },
    {
      title: '部门拼音',
      dataIndex: 'deptPy',
      key: 'deptPy',
    },
    {
      title: '联系方式',
      dataIndex: 'phone',
      key: 'phone',
    },
    {
      title: 'Action',
      key: 'action',
      render: (text, record, index) => {
        let confirmTitle = '是否删除' + record.deptName;
        let confirmTitle1 = '是否编辑' + record.deptName;
        return (
          <Space size="middle">
            {/* 编辑确认框 */}
            <Popconfirm
              title={confirmTitle1}
              onConfirm={() => {
                this.handleEdit(record);
              }}
              okText="Yes"
              cancelText="No"
            >
              <a>Edit</a>
            </Popconfirm>

            {/* 删除确认框 */}
            <Popconfirm
              title={confirmTitle}
              onConfirm={() => {
                this.handleDelete(record);
              }}
              okText="Yes"
              cancelText="No"
            >
              <a>Delete</a>
            </Popconfirm>
          </Space>
        );
      },
    },
  ];

  // 显示抽屉
  showDrawer = () => {
    this.setState({
      drawerVisible: true,
      currentRecord: null, // 清空当前操作的记录
    });
  };

  // 关闭抽屉
  onDrawerClose = () => {
    this.setState({
      drawerVisible: false,
      currentRecord: null, // 清空当前操作的记录
    });
  };

  // 渲染组件
  render() {
    const { drawerVisible, currentRecord, tableData } = this.state; // 从状态中解构需要的数据

    return (
      <>
        {/* 新增部门按钮 */}
        <Button type="primary" onClick={this.showDrawer}>
          新增部门
        </Button>
        {/* 部门列表表格 */}
        <Table
          dataSource={tableData}
          columns={this.columns}
          rowKey="deptId"
        />

        {/* 抽屉组件，用于新增或编辑部门 */}
        <Drawer
          title={currentRecord ? '编辑部门' : '新增部门'} // 根据是否有currentRecord来设置标题
          placement="right"
          width={'50%'}
          onClose={this.onDrawerClose}
          open={drawerVisible}
          destroyOnClose={true} // 关闭时销毁子元素，防止状态保留
        >
          {/* DeptForm组件，用于填写部门信息 */}
          <DeptForm
            editData={currentRecord} // 传递当前操作的记录，用于编辑模式
            refreshList={this.refreshData}
            closeDrawer={this.onDrawerClose}
          />
        </Drawer>
      </>
    );
  } }

export default Demo2;
